<template>
  <div style="width:300px;">
    <el-upload
        class="upload-demo"
        :action="$uploadImg"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-upload="beforeAvatarUpload"
        :show-file-list="showfilelist"
        multiple
        :accept="accept"
        :file-list="fileList"
        :limit="Number(number)">
      <el-button size="small" class="el-icon-upload">点击上传相应文件</el-button>
    </el-upload>
    <AuxiliaryModule @getInformation="getInformation" :typeData="fileType" v-if="sycnStatus"></AuxiliaryModule>
  </div>
</template>

<script>
export default {
  props: ['number', 'sycnStatus', 'fileType','fileListPush','showfile'],
  name: "uploadFileModle",
  data() {
    return {
      typeData: 'file_add',
      fileList: [],
      showfilelist: true,
      accept: '.doc,.docx,.xls,.xlsx,.rar,.zip,.7z,.jpg,.jpeg,.png,.mp4,.pdf'
    }
  },
  watch:{
    fileListPush(){
      if(this.fileListPush && this.fileListPush.length){
        console.log('this.fileListPush12:', this.fileListPush)
        let list = JSON.parse(JSON.stringify(this.fileListPush))
        if(!this.fileListPush.url){
          for(let item of list){
            item.url = item.src
          }
        }
        console.log('删除内容list：', list)
        this.fileList = JSON.parse(JSON.stringify(list))
      }else{
        this.fileList = []
      }
    },
  },
  created() {
    if(this.showfile!==undefined){
      console.log('showfile:', this.showfile)
      this.showfilelist = this.showfile
    }
    // if(this.fileListPush && this.fileListPush.length){
    //   console.log('this.fileListPush:', this.fileListPush)
    //   let list = JSON.parse(JSON.stringify(this.fileListPush))
    //   if(!this.fileListPush.url){
    //     for(let item of list){
    //       item.url = item.src
    //     }
    //   }
    //   console.log('删除内容list：', list)
    //   this.fileList = JSON.parse(JSON.stringify(list))
    // }
  },
  methods: {
    getInit() {
      this.fileList = []
    },
    beforeAvatarUpload(file) {
      console.log('上传数据excel：', file)
      // 此处需要限制上传数据的格式
      // const isJPG = file.type === 'image/jpeg';
      // const isLt2M = file.size / 1024 / 1024 < 2;
      //
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!');
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!');
      // }
      // return isJPG && isLt2M;
    },
    handleSuccess(response, file, fileList) {
      for (let item of fileList) {
        item.src = item.response ? item.response.data.name : item.url
      }
      this.fileList = JSON.parse(JSON.stringify(fileList))
      this.$emit('getFile', fileList)
    },
    handleRemove(file, fileList) {
      console.log('删除内容：', fileList)
      for (let item of fileList) {
        item.src = item.response ? item.response.data.pic : item.url
      }
      this.$emit('getFile', fileList)
    },
    // 同步数据
    getInformation(option) {
      let list_ = JSON.parse(JSON.stringify(this.fileList))
      let list_a = []
      console.log('old======>', list_)
      console.log('new======>', option)
      for (let item of option) {
        if (item.type === 'img' && item.number === 1) {
          this.foodEdit.pic = item.value[0].url
        }
        if (item.type === 'img' && item.number !== 1) {
          for (let item_list of item.value) {
            let add_status = true
            for(let img of list_){
              if(item_list.url===img.url){
                add_status = false
              }
            }
            if(add_status){
              list_a.push(item_list)
            }
          }
          this.fileList = list_.concat(list_a)
        }
      }
      let list = JSON.parse(JSON.stringify(this.fileList))
      for (let item of list) {
        item.src = item.response ? item.response.data.pic : item.url
      }
      this.$emit('getFile', list)
    }
  }
}
</script>

<style scoped>

</style>
